package struts.Servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import struts.action.Action;
import struts.form.Form;
import struts.form.FullBean;
import struts.form.XmlBean;

public class ActionServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = req.getServletPath().split("\\.")[0];
		Map<String,XmlBean> xmlBean = (Map<String,XmlBean>)this.getServletContext().getAttribute("struts-path");
		XmlBean bean = xmlBean.get(path);
		String url = "";
		try {
			Form form = FullBean.getBean(bean.getClassName(), req);
			String actionName = bean.getActionType();
			Class clazz = Class.forName(actionName);
			Action action = (Action)clazz.newInstance();
			url = action.execute(bean.getForwardMap(), form, req);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestDispatcher dis = req.getRequestDispatcher(url);
		dis.forward(req, resp);
	}
}
